package com.hph.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hph.health.common.BaseResponse;
import com.hph.health.common.ErrorCode;
import com.hph.health.common.ResultUtils;
import com.hph.health.exception.BusinessException;
import com.hph.health.exception.ThrowUtils;
import com.hph.health.mapper.CartMapper;
import com.hph.health.model.dto.cart.CartAddRequest;
import com.hph.health.model.dto.cart.CartRequest;
import com.hph.health.model.dto.user.DeleteRequest;
import com.hph.health.model.entity.Cart;
import com.hph.health.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hanph
 * 购物车接口
 * @from haut
 */
@RestController
@RequestMapping("/card")
@Slf4j
public class CartController {

    @Resource
    private CartService cartService;

    @Resource
    private CartMapper cartMapper;

    /**
     * 获取购物车列表
     *
     * @param cartRequest
     * @return
     */
    @PostMapping("/list")
    public BaseResponse<List<Cart>> listProductByPage(@RequestBody CartRequest cartRequest) {
        String userId = cartRequest.getUserId();
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userId),"user_id", userId);
        List<Cart> list = cartService.list(queryWrapper);
        return ResultUtils.success(list);
    }

    /**
     *  新增购物车
     *
     * @param cartAddRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addProduct(@RequestBody CartAddRequest cartAddRequest) {
        if (cartAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Cart cart = new Cart();
        BeanUtils.copyProperties(cartAddRequest, cart);
        // 不能重复加入购物车
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", cart.getUserId());
        queryWrapper.eq("product_id", cart.getProductId());
        long count = cartMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "此商品您已加入购物车");
        }
        boolean result = cartService.save(cart);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(cart.getId());
    }

    /**
     * 删除购物车
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteProduct(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(cartService.removeById(deleteRequest.getId()));
    }

}
